﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using Microsoft.Win32;

namespace TauschFormat2
{
    /// <summary>
    /// Interaktionslogik für Styles.xaml
    /// </summary>
    public partial class Styles : Window
    {
        StylesDBDataSet styleDB = new StylesDBDataSet();
        StylesDBDataSetTableAdapters.StylesTableAdapter styleDBAdapter = new StylesDBDataSetTableAdapters.StylesTableAdapter();
        string oldname = "";

        public Styles()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            loaddata();
        }

        private void lbStyles_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            styleDBAdapter.Fill(styleDB.Styles);
            try
            {
                var styles = from r in styleDB.Styles where r.name == lbStyles.SelectedItem.ToString() select new { r.name, r.style };

                foreach (var style in styles)
                {
                    txtName.Text = style.name;
                    oldname = style.name;
                    txtStyle.Text = style.style;
                    break; //Nicht elegant, funktionert aber. hard limit 1 sozusagen xD
                }
            }
            catch (Exception ex)
            {
                //Actually, do nothing.
            }
        }

        private void bttnStyleSpeichern_Click(object sender, RoutedEventArgs e)
        {
            if (lbStyles.SelectedItem == null)
            {
                MessageBox.Show("Keinen Style ausgewählt","Fehler",MessageBoxButton.OK,MessageBoxImage.Error);
                return;
            }
            styleDBAdapter.Fill(styleDB.Styles);
            DataRow[] stylesRow = styleDB.Styles.Select("name = '" + oldname + "'");

            stylesRow[0]["name"] = txtName.Text;
            stylesRow[0]["style"] = txtStyle.Text;

            styleDBAdapter.Update(styleDB.Styles);
            loaddata();
        }
        private void loaddata()
        {
            lbStyles.Items.Clear();
            styleDBAdapter.Fill(styleDB.Styles);
            foreach (DataRow r in styleDB.Styles.Rows)
            {
                lbStyles.Items.Add(r["name"]);
            }
        }

        private void bttnNeuerStyle_Click(object sender, RoutedEventArgs e)
        {
            txtName.Text = "";
            txtStyle.Text = "";
            styleDBAdapter.Fill(styleDB.Styles);
            styleDB.Styles.AddStylesRow("Neuer Style", "");
            styleDBAdapter.Update(styleDB.Styles);
            loaddata();
        }

        private void bttnStyleLoeschen_Click(object sender, RoutedEventArgs e)
        {
            if (lbStyles.SelectedItem == null)
            {
                MessageBox.Show("Keinen Style ausgewählt", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            styleDBAdapter.Delete(lbStyles.SelectedItem.ToString());
            loaddata();
        }

        private void bttnStyleExportieren_Click(object sender, RoutedEventArgs e)
        {
            if (lbStyles.SelectedItem == null)
            {
                MessageBox.Show("Keinen Style ausgewählt", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            styleDBAdapter.Fill(styleDB.Styles);
            DataRow[] rs = styleDB.Styles.Select("name = '" + lbStyles.SelectedItem.ToString()+"'");
            StylesDBDataSet tdb = new StylesDBDataSet();
            tdb.Styles.AddStylesRow((string)rs[0]["name"], (string)rs[0]["style"]);
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Template-Dateien (*.ttmpl)|*.ttmpl";
            sfd.FileName = (string)rs[0]["name"];
            sfd.ShowDialog();
            tdb.WriteXml(sfd.FileName);
        }

        private void bttnStyleImportieren_Click(object sender, RoutedEventArgs e)
        {
            StylesDBDataSet tdb = new StylesDBDataSet();
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Template-Dateien (*.ttmpl)|*.ttmpl";
            ofd.ShowDialog();
            tdb.ReadXml(ofd.FileName);
            styleDBAdapter.Fill(styleDB.Styles);
            foreach (DataRow r in tdb.Styles.Rows)
            {
                styleDB.Styles.AddStylesRow((string)r[1], (string)r[2]);
            }
            styleDBAdapter.Update(styleDB.Styles);
            loaddata();
        }
    }
}
